<?
require_once("inc_security.php");
//check quyền them sua xoa
checkAddEdit("edit");
if(!$admin_role['rol_edit']){
    die('Bạn không có quyền truy cập trang này !');
}
$returnurl = base64_decode(getValue("url","str","GET",base64_encode("listing.php")));

//Khai bao Bien
$errorMsg		=	"";
$errorMsgAll	=	"";
$iQuick = getValue("iQuick","str","POST","");
$record_id = getValue("record_id", "arr", "POST", "");
//check_permission_news($record_id);
//Bien quick edit tu danh sach cac tin ko lay duoc anh
$updateimage	=	getValue("updateimage", "str", "POST", "");


if($iQuick == 'update'){
	$total_record	=	count($record_id);
	if($total_record > 0){
		for($i = 0; $i < $total_record; $i++){
			//Call Class generate_form();
			$myform = new generate_form();
			//Loại bỏ chuc nang thay the Tag Html
			$myform->removeHTML(0);
			//Insert to database
			//Neu quickedit tu danh sach cac tin ko lay duoc anh
			if($updateimage == ""){
				$myform->add('new_cat_id', "new_cat_id_" . $record_id[$i], 1, 0, '', 0, '0', 0, '');
				$myform->add('new_tags', "new_tags" . $record_id[$i], 0, 0, '', 0, '', 0, '');
				$myform->add('new_title', "new_title" . $record_id[$i], 0, 0, '', 0, '', 0, '');
				$myform->add('new_coin', "new_coin" . $record_id[$i], 0, 0, '', 0, '', 0, '');
			}

			//Add table
			$myform->addTable($fs_table);
			
			$upload_pic = new upload("new_picture_" . $record_id[$i], $fs_filepath, $fs_extension, $fs_filesize);
			$file_name	=	$upload_pic->file_name;
            $upload_poster = new upload("new_poster_" . $record_id[$i], $fs_filepath, $fs_extension, $fs_filesize);
			$poster_name	=	$upload_poster->file_name;
			//echo	$file_name;
			if($file_name != ""){
				$myform->add("new_picture","file_name", 0, 1, "", 0, "", 0, "");
				foreach($arr_resize as $type => $arr){
					resize_image($fs_filepath, $file_name, $arr["width"], $arr["height"], $arr["quality"], $type);
				}
				delete_file($fs_table,$id_field,$record_id[$i],"new_picture",$fs_filepath);
			}
            
            if($poster_name != ""){
				$myform->add("new_poster","poster_name", 0, 1, "", 0, "", 0, "");
				foreach($arr_resize as $type => $arr){
					resize_image($fs_filepath, $poster_name, $arr["width"], $arr["height"], $arr["quality"], $type);
				}
				delete_file($fs_table,$id_field,$record_id[$i],"new_poster",$fs_filepath);
			}

			//Check Error!
			$errorMsg .= $upload_pic->show_warning_error() . $upload_poster->show_warning_error();			
			$errorMsg .= $myform->checkdata($id_field, $record_id[$i]);
			//Check loi cua tat ca cac ban ghi duoc sua
			$errorMsgAll	.=	$errorMsg;
			
			if($errorMsg == ""){
				$db_ex = new db_execute($myform->generate_update_SQL($id_field, $record_id[$i]));
				//echo $myform->generate_update_SQL($id_field, $record_id[$i]) . "<br>";
				unset($db_ex);
				//echo $errorMsg;
				
			}else{
				echo $record_id[$i] . " : " . $errorMsg . "</br>";
			}
			unset($myform);
		}
	}
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
	echo "Đang cập nhật dữ liệu !";
	if($errorMsgAll == ""){
		redirect($returnurl);
	}
}
?>